package com.lmuse.xmr.foodspotting.ui.discover;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.dexafree.materialList.card.Card;
import com.dexafree.materialList.card.CardProvider;
import com.dexafree.materialList.card.OnActionClickListener;
import com.dexafree.materialList.card.action.TextViewAction;
import com.dexafree.materialList.listeners.OnDismissCallback;
import com.dexafree.materialList.listeners.RecyclerItemClickListener;
import com.dexafree.materialList.view.MaterialListView;
import com.lmuse.xmr.foodspotting.R;
import com.lmuse.xmr.foodspotting.db.Contract;
import com.lmuse.xmr.foodspotting.db.DiscoverPresenterImpl;
import com.lmuse.xmr.foodspotting.db.FollowPresenterImpl;
import com.lmuse.xmr.foodspotting.db.UserPresenterImpl;
import com.lmuse.xmr.foodspotting.model.Recipe;
import com.lmuse.xmr.foodspotting.model.User;
import com.lmuse.xmr.foodspotting.ui.recipe.RecipeActivity;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;

public class DiscoverFragment extends Fragment implements Contract.DiscoverView, Contract.FollowView, Contract.UserView{

    private MaterialListView cardList;

    private Contract.DiscoverPresenter discoverPresenter;
    private Contract.FollowPresenter followPresenter;
    private Contract.UserPresenter userPresenter;

    private List<Recipe> recipeList;
    private List<User> userList;
    private String author;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discover, container, false);
        discoverPresenter = new DiscoverPresenterImpl(this);
        followPresenter = new FollowPresenterImpl(this);
        userPresenter = new UserPresenterImpl(this);
        initView(view);
        followPresenter.queryFollowings();
        userPresenter.queryByKey("nickname");
        return view;
    }

    void initView(View view) {
        cardList = (MaterialListView) view.findViewById(R.id.cardlist_discover);
        cardList.addOnItemTouchListener(new CardClickListener());
        cardList.setOnDismissCallback(new CardDismissCallBack());
        recipeList = new ArrayList<>();
        userList = new ArrayList<>();
    }


    class CardClickListener implements RecyclerItemClickListener.OnItemClickListener {

        @Override
        public void onItemClick(@NonNull Card card, int position) {
            Intent startIntent = new Intent(getActivity(),
                    RecipeActivity.class);
            startIntent.putExtra(com.lmuse.xmr.foodspotting.constants.Recipe.ID,
                    recipeList.get(position).getObjectId());
            startActivity(startIntent);
        }

        @Override
        public void onItemLongClick(@NonNull Card card, int position) {

        }
    }

    class CardDismissCallBack implements OnDismissCallback {
        @Override
        public void onDismiss(@NonNull Card card, int position) {
            /*int count = (page + 1) * limit - 2;
            if (position == count) {
                Toast.makeText(getActivity(), "当前第" + count + "个", Toast.LENGTH_SHORT).show();
                page++;
                recipePresenter.queryRecipe("label", labelTitle.getText().toString(), limit, page);
            }*/
        }
    }

    @Override
    public void onAddFollowingSuccess() {

    }

    @Override
    public void onAddFollowingFailed(String msg) {

    }

    @Override
    public void onUnFollowingSuccess() {

    }

    @Override
    public void onUnFollowingFailed(String msg) {

    }

    @Override
    public void onQueryFollowingSuccess(List<User> userList) {
        List<String> objectIdList = new ArrayList<>();
        for (User user : userList) {
            objectIdList.add(user.getObjectId());
        }
        discoverPresenter.queryDiscoverRecipe(objectIdList);
    }

    @Override
    public void onQueryFollowingFailed(String msg) {
        Toast.makeText(getActivity(), "获取关注的用户失败！(" + msg + ")", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onQueryFollowerSuccess(List<User> userList) {

    }

    @Override
    public void onQueryFollowerFailed(String msg) {

    }

    @Override
    public void onQueryFollowerAmountSuccess(Integer num) {

    }

    @Override
    public void onQueryFollowerAmountFailed(String msg) {

    }

    @Override
    public void onQueryDiscoverRecipeSuccess(List<Recipe> recipeList) {

        this.recipeList = recipeList;

        for (Recipe recipe : recipeList) {

            for (User user : userList) {
                if (recipe.getAuthor().getObjectId().equals(user.getObjectId())) {
                    author = user.getNickname();
                }
            }

            Card card = new Card.Builder(getActivity())
                    .withProvider(new CardProvider())
                    .setLayout(R.layout.material_image_with_buttons_card)
                    .setTitle(recipe.getTitle())
                    .setTitleColor(getResources().getColor(R.color.colorWhite))
                    .setDescription(recipe.getUpdatedAt())
                    .setDrawable(recipe.getCoverUrl())
                    .addAction(R.id.left_text_button, new TextViewAction(getActivity())
                            .setText(author)
                            .setTextResourceColor(R.color.colorMainGreen)
                            .setListener(new OnActionClickListener() {
                                @Override
                                public void onActionClicked(View view, Card card) {
                                }
                            }))
                    .endConfig()
                    .build();
            cardList.getAdapter().add(card);
        }
        cardList.smoothScrollToPosition(0);
    }

    @Override
    public void onQueryDiscoverRecipeFailed(String msg) {
        Toast.makeText(getActivity(), "请求发现列表失败！(" + msg + ")", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSignUpSuccess(User user) {

    }

    @Override
    public void onSignUpFailed(String msg) {

    }

    @Override
    public void onSignInSuccess(User user) {

    }

    @Override
    public void onSignInFailed(String msg) {

    }

    @Override
    public void onUserInfoCache(User user) {

    }

    @Override
    public void onUpdateUserSuccess() {

    }

    @Override
    public void onUpdateUserFailed(String msg) {

    }

    @Override
    public void onRequestEmailVerifyResult(BmobException e) {

    }

    @Override
    public void onResetPasswordByEmailResult(BmobException e) {

    }

    @Override
    public void onQueryUserByIdSuccess(User user) {

    }

    @Override
    public void onQueryUserByIdFailed(String msg) {
//        num++;
    }

    @Override
    public void onQueryUserAmountSuccess(int size) {

    }

    @Override
    public void onQueryUserAmountFailed(String msg) {

    }

    @Override
    public void onQueryByKeySuccess(List<User> userList) {
        this.userList = userList;
    }

    @Override
    public void onQueryByKeyFailed(String msg) {
        Toast.makeText(getActivity(), "请求失败！", Toast.LENGTH_SHORT).show();
    }
}
